package org.file_service.controller;


import org.apache.commons.lang.RandomStringUtils;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.file_service.Analysis;
import org.file_service.lang.Result;
import org.file_service.service.GenerateService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.net.URISyntaxException;

@CrossOrigin
@Controller
public class dataGenerateController {

    @Autowired
    GenerateService generateService;

    @Autowired
    FileSystem fileSystem;

    @RequestMapping("/dataGenerate")
    @ResponseBody
    public Result dataGenerate(int userId, String operator, int times, String district, String rate) {
        return generateService.DataGenerate(userId, operator, times, district, rate);
    }

    @RequestMapping("/analysis")
    @ResponseBody
    public Result test(int historyId, int userId, String path) {
        Analysis.analysis(historyId, userId);
        return Result.suc("Test分析结束");
    }

    @RequestMapping("/mkdir")
    @ResponseBody
    public Result test() throws IOException, URISyntaxException, InterruptedException {
        reName();
        return Result.suc("Test分析结束");
    }

    public void reName(){
        String s = "/zz/test."+ RandomStringUtils.random(15, "abcdefghfrbf3452e32423131");
        try {
            fileSystem.rename(new Path("/zz/tmp"), new Path(s));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
